package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.*;
import com.ws.ssmzongjie3.service.Acl_PermissionService;
import com.ws.ssmzongjie3.service.Acl_Role_PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/menu")
public class Acl_PermissionController {
    @Autowired
    private Acl_PermissionService acl_PermissionService;
    @Autowired
    private Acl_Role_PermissionService acl_Role_PermissionService;
    @RequestMapping("/list")
    public String select(Model model, ProVo proVo, Acl_Role acl_role, Acl_Permission acl_permission){
        System.out.println("links:"+acl_permission);



        List<Acl_Permission> aclPermissions = acl_PermissionService.listAcl_Permission(acl_permission);
        List<Integer> integers = acl_PermissionService.listPermissionsByRoleId(acl_role);
        ArrayList<Acl_Permission> gai = gai(aclPermissions, integers);
        model.addAttribute("gai",gai);
        model.addAttribute("acl_permission",acl_permission);
        return "WEB-INF/admin/menu/list";
    }
    @RequestMapping("/add")
    public String add(Model model, Acl_Permission acl_permission){
        model.addAttribute("acl_permission",acl_permission);
        return "WEB-INF/admin/menu/add";
    }
    @RequestMapping("/insert")
    public String insert(Acl_Permission acl_permission){
        acl_PermissionService.insertAcl_Permission(acl_permission);
        return "redirect:/menu/list";
    }

    @RequestMapping("/delete")
    public String delete(Acl_Permission acl_permission){
        acl_PermissionService.deleteAcl_PermissionById(acl_permission);
        return "redirect:/menu/list";
    }

    @RequestMapping("/huixian")
    public String huixian(Acl_Permission acl_permission, Model model) {
        Acl_Permission aclPermission = acl_PermissionService.getAcl_PermissionById(acl_permission);
        model.addAttribute("acl_permission",aclPermission);
        return "/WEB-INF/admin/menu/update";
    }

    @RequestMapping("/update")
    public String update(Acl_Permission acl_permission){
        acl_PermissionService.updateAcl_Permission(acl_permission);
        return "redirect:/menu/list";
    }

    @RequestMapping("/addPermission")
    public String addPermission(Acl_Permission acl_permission, Model model,Acl_Role acl_role){
        List<Acl_Permission> aclPermissions = acl_PermissionService.listAcl_Permission(acl_permission);
        List<Integer> integers = acl_PermissionService.listPermissionsByRoleId(acl_role);
        ArrayList<Acl_Permission> gai = gai(aclPermissions, integers);
        model.addAttribute("aclPermissions",gai);
        model.addAttribute("acl_role",acl_role);

        return "/WEB-INF/admin/acl_role/permission";
    }
    @RequestMapping("/updatePermission")
    public String updatePermission(Acl_Role_Permission acl_role_permission, Integer [] perm_id){
        acl_Role_PermissionService.deleteAcl_Role_PermissionById(acl_role_permission);
        
        for (Integer integer : perm_id) {
            acl_role_permission.setPerm_id(integer);
            acl_Role_PermissionService.insertAcl_Role_Permission(acl_role_permission);
        }
        return "redirect:/acl_role/list";
    }
    public ArrayList<Acl_Permission> gai(List<Acl_Permission> aclPermissions, List<Integer> integers){
        ArrayList<Acl_Permission> aclPermissions1 = new ArrayList<>();
        for (Acl_Permission aclPermission : aclPermissions) {
            if (Integer.parseInt(aclPermission.getPid()) == 0){
                aclPermissions1.add(aclPermission);
            }
        }
        for (Acl_Permission aclPermission : aclPermissions) {
            if (Integer.parseInt(aclPermission.getPid()) != 0){
                for (Acl_Permission aclPermission1 : aclPermissions1) {
                    if (Integer.parseInt(aclPermission.getPid()) == aclPermission1.getPerm_id()){
                        aclPermission1.getChildren().add(aclPermission);

                    }
                }
            }
        }
        for (Acl_Permission aclPermission : aclPermissions1) {
            for (Integer integer : integers) {
                if (Objects.equals(aclPermission.getPerm_id(), integer)){
                    aclPermission.setChecked(true);
                }
                for (Acl_Permission aclPermission1 : aclPermission.getChildren()) {
                    if (Objects.equals(aclPermission1.getPerm_id(), integer)){
                        aclPermission1.setChecked(true);
                    }
                }
            }
        }
        return aclPermissions1;
    }

}
